package com.mnt.gui.base.buildfactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;

import com.mnt.gui.base.buildfactory.expand.FileDragInHandle;
import com.mnt.gui.base.buildfactory.expand.FileDragOutHandle;

/**
 * control function expand factory
 * <p>
 * 1.add drag event<br/>
 * </p>
 * @author cico.mnt
 * @date 2014-4-27
 */
public class FunctionExpandFactory {
	private FunctionExpandFactory() {
		//empty
	}
	
	/**
	 * add drag file function to the node
	 * @param node  is add listener node
	 * @param dragInHandle parseFile handle
	 * @param suffixs support suffix
	 */
	public static final void buildDragFileIn(Node node, final FileDragInHandle dragInHandle, final String... suffixs) {
		node.setOnDragOver(new EventHandler<DragEvent>() {
			public void handle(DragEvent event) {
				Dragboard dragboard = event.getDragboard();
				if (dragboard.hasFiles()) {
					File file = dragboard.getFiles().get(0);
					if(null != suffixs && suffixs.length > 0 ) {
						filter : for(String suffix : suffixs) { // find the suffix break circulate
							if(file.getAbsolutePath().endsWith(suffix)) {
								event.acceptTransferModes(TransferMode.COPY);
								break filter;
							}
						}
					} else {
						event.acceptTransferModes(TransferMode.COPY);
					}
				}

			}
		});
		
		node.setOnDragDropped(new EventHandler<DragEvent>() {
			public void handle(DragEvent event) {
				// get drag enter file
				Dragboard dragboard = event.getDragboard();
				if (event.isAccepted()) {
					File file = dragboard.getFiles().get(0);
					dragInHandle.parseFile(file);
				}
			}
		});
		
	}
	
	
	/**
	 * add drag out to file listener with node
	 * @param node 
	 * @param isCreateFileCheckFunction input check is generate file function 
	 * @param fileName generate file name
	 * @param outHandel generate rude handle
	 */
	public static final <N extends Node>void buildDragFileOut(final N node, final boolean isCreateFileCheckFunction, final FileDragOutHandle<N> outHandel, final String... fileNames) {
		final ClipboardContent content = new ClipboardContent();
		node.setOnDragDetected(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent event) {
				if(isCreateFileCheckFunction) {
					Dragboard dragboard = node.startDragAndDrop(TransferMode.ANY);
					
					File file;
					final List<File> files = new ArrayList<File>(fileNames.length);
					for (String fileName : fileNames) {
						file = new File(fileName);
						try {
							file.createNewFile();
						} catch (IOException e) {
							e.printStackTrace();
						}
						files.add(file);
						if(file.isFile()) {
							file.deleteOnExit();
						}
					}
					final File[] fileArray = new File[files.size()];
					outHandel.fileCreate(node, files.toArray(fileArray));
					content.putFiles(files);
					dragboard.setContent(content);
					event.consume();
				}
			}
		});
	}
	
}
